package com.test.mvcservice.controller;

import com.test.mvcservice.dao.UserMapper;
import com.test.mvcservice.domain.ResDTO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 20161021 on 2017/11/28.
 */
@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "getUserName", method = RequestMethod.POST)
    public String getUserName(int id){
        return userMapper.selectUserName(id);
    }

    @RequestMapping(value = "testIntArray", method = RequestMethod.POST)
    public Integer testIntArray(Integer[] id){
        return 1;
    }

    @RequestMapping(value = "testList", method = RequestMethod.POST)
    public ResDTO testList(@RequestParam List<Integer> list){
        ResDTO res = new ResDTO();
        res.setFlag(1);
        res.setMessage("请求成功");
        res.setData(list);
        return res;
    }

    @RequestMapping(value = "testMap", method = RequestMethod.POST)
    public ResDTO testMap(@RequestParam Map<String,Object> map){
        ResDTO res = new ResDTO();
        res.setFlag(1);
        res.setMessage("请求成功");
        res.setData(map);
        return res;
    }

    @RequestMapping(value = "testDate", method = RequestMethod.POST)
    public ResDTO testChineseResDTO(Date date){
        ResDTO res = new ResDTO();
        res.setData("测试");
        return res;
    }

    @RequestMapping(value = "testMessageConvert", method = RequestMethod.POST)
    public String testMessageConvert(@RequestBody String id){
        return "SUCCESS";
    }

    @RequestMapping(value = "testId", method = RequestMethod.POST)
    public String testId(Integer id){
        return "SUCCESS";
    }

    @RequestMapping("/defaultParameter")
    public ModelAndView defaultParameter(HttpServletRequest request, HttpServletResponse response,
                                         HttpSession session, Model model, ModelMap modelMap) throws Exception{
        request.setAttribute("requestParameter", "request类型");
        response.getWriter().write("response");
        session.setAttribute("sessionParameter", "session类型");
        //ModelMap是Model接口的一个实现类，作用是将Model数据填充到request域
        //即使使用Model接口，其内部绑定还是由ModelMap来实现
        model.addAttribute("modelParameter", "model类型");
        modelMap.addAttribute("modelMapParameter", "modelMap类型");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

}
